package com.lwl.blog.service.impl;

import com.lwl.blog.core.dao.IBaseDao;
import com.lwl.blog.core.service.BaseServiceImpl;
import com.lwl.blog.dao.UserDao;
import com.lwl.blog.dao.impl.UserDaoImpl;
import com.lwl.blog.entity.UserEntity;
import com.lwl.blog.service.UserService;
import com.lwl.blog.utils.UUIDUtils;

import java.sql.SQLException;
import java.util.Date;

/**
 * 帖子用户业务层实现类
 */
public class UserServiceImpl extends BaseServiceImpl<UserEntity> implements UserService {
    //调用dao层
    private UserDao userDao = new UserDaoImpl();

    @Override
    protected IBaseDao<UserEntity> getBaseDao() {
        return userDao;
    }

    @Override
    public UserEntity login(String username, String password) throws SQLException {
        //1.根据用户名查询是否存在
        UserEntity u = userDao.selByUname(username);
        //2.判断用户是否存在
        if (u == null) {
            return null;
        }
        //3.对比秘密是否正确
//        if (!PasswordUtils.check(password, u.getPassword())) {
//            return null;
//        }
        if (!password.equals(u.getPassword()) || password == null) {
            return null;
        }
        return u;

    }

    //用户注册
    @Override
    public String register(UserEntity u) throws SQLException {
        //根据名字查询
        UserEntity us=userDao.selByUname(u.getUsername());
        if (us!=null){
            return "用户名已存在";
        }
        //添加默认状态
        u.setStatus(0);
        u.setCreateTime(new Date());
        u.setActivationCode(UUIDUtils.getId());
        u.setType(1);
        //入库
        int save = userDao.insert(u);
        if (save>0){
            return "注册成功";
        }
        return "注册失败";
    }

    @Override
    public UserEntity selectActCode(String code) throws SQLException {
        System.out.println("UserServiceImpl.selectActCode："+code);
        return userDao.selectActCode(code);
    }
}
